package com.balance;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
//import com.baomidou.mybatisplus.generator.AutoGenerator;


import java.sql.Types;
import java.util.Collections;
import java.util.Scanner;

/**
 * @Title: GeneratorApplication
 * @Author 小东
 * @Package com.balance
 * @Date 2025/3/28 20:12
 * @description: 生成代码
 */
public class GeneratorApplication {
    public static void main(String[] args) {
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        String url ="jdbc:mysql://172.20.10.2:3307/balance?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&zeroDateTimeBehavior=convertToNull&useLegacyDatetimeCode=false&rewriteBatchedStatements=true&serverTimezone=GMT%2B8";
        String username =  "root";
        String strpassword = "root";
        FastAutoGenerator.create(url, username, strpassword)
                .globalConfig(builder -> {
                    builder.author("dong") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir("E:/code/design_by_dong/balance_counter/balance-generator/src/main/resources/GeneratorCode"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.balance.mapper.sys") // 设置父包名
                                .moduleName("balance_counter") // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.xml, "E:/code/design_by_dong/balance_counter/balance-generator/src/main/resources/GeneratorCode/mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("sys_config") // 设置需要生成的表名
//                                .addTablePrefix("sys_") // 设置过滤表前缀
                )
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}